package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public PageResponse<RoleVo> selectByPage(RoleDto roleDto, Integer pageNum, Integer pageSize) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        PageHelper.startPage(pageNum, pageSize);
        Page page = (Page) roleMapper.selectByPage(role);
        PageResponse<RoleVo> of = PageResponse.of(page, RoleVo.class);
        return of;
    }

    @Override
    public List<RoleVo> selectList() {
        List<RoleVo> list = roleMapper.selectByPage(null);
        return list;
    }

    @Override
    public Boolean addRole(RoleDto roleDto) {
        Integer row = null;
        try {
            Role role = BeanUtil.toBean(roleDto, Role.class);
            row = roleMapper.insertRole(role);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.ROLE_NAME_EXIST_ERROR);
        }
        if (row > 0) {
            return true;
        }
        return false;
    }


    @Override
    public Boolean updateRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);


        // 角色若已经分配用户，不允许禁用
        Integer count = userRoleMapper.selectByRoleId(roleDto.getId());

        if(count>0){
            throw new BaseException(BasicEnum.ROLE_DISTRIBUTED_ERROR);
        }

        if (roleDto.getCheckedResourceNos() == null) {
            Integer row = roleMapper.updateRole(role);
            if (row > 0) {
                return true;
            }
        }

        //先根据id删除所有记录
        roleResourceMapper.deleteByRoleId(roleDto.getId());

        if (roleDto.getCheckedResourceNos().length == 0) {
            return true;
        }

        String[] resourceNos = roleDto.getCheckedResourceNos();

        List<RoleResource> roleResources = Arrays.stream(resourceNos).map(resourceNo -> {
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(roleDto.getId());
            roleResource.setResourceNo(resourceNo);
            roleResource.setDataState(roleDto.getDataState());
            return roleResource;
        }).collect(Collectors.toList());

        Integer row = roleResourceMapper.insertRoleResources(roleResources);

        return true;
    }

    @Override
    public List<String> findCheckedResourcesByRoleId(Long roleId) {
        List<String> resources = roleResourceMapper.selectResourcesByRoleId(roleId);
        return resources;
    }


    @Override
    public void deleteRole(String roleId) {
        // 角色若已经分配用户，不允许删除
        Integer count = userRoleMapper.selectByRoleId(Long.valueOf(roleId));

        if(count>0){
            throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
        }
        roleMapper.deleteRole(roleId);
    }
}
